package com.fairytask.ui
{
	import com.fairytask.data.Modulator;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.net.CustomToClientEvent;
	import com.fairytask.ui.base.FrameUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	/**
	 *商店界面 
	 * @author pangj
	 * 
	 */
	public class ShopFrameUI extends FrameUI
	{
		private static var _self :ShopFrameUI=null;
			
		public  var col           :int;        //商品显示列
		public  var row           :int;        //商品显示行
		public  var maxPage       :int;        //最大页码
		public  var thePage       :int;        //当前页码
		public  var amountUI      :InputAmountUI;
		
		public  var preBtn        :Button;    //上一页 按钮
		public  var nextBtn       :Button;    //下一页 按钮
		public  var pageLabel     :Label;     //页签
		public  var pageLabBG     :MovieClip  //页签底
		
		//模型
		private var _mode         :Modulator = Modulator.instance;
		private var _goods        :Array=[];     //本页商品
		
		private var goodsInfo     :Object        //商品信息
		
		//人物
		private var capital       :Array=[];     //玩家的金钱资本
		private var npcID	       :Point;         //与玩家交易的npc编号 

		public static function getInstance():ShopFrameUI
		{
			if(_self==null)
			{
				_self=new ShopFrameUI();
			}
			return _self;
		}
		
		public function ShopFrameUI(popUI:Boolean=true)
		{
			super(popUI);
			initprop();
			initEvent();
		}
		/**
		 * 绑定人物金钱属性 
		 * 
		 */		
		private function initprop():void
		{
			//从包裹中绑定金钱资本
			/*bindManager.BindViewProp(_mode.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Silver",getCapital);
			bindManager.BindViewProp(_mode.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"BindSilver",getCapital); 
			bindManager.BindViewProp(_mode.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Gold",getCapital);
			bindManager.BindViewProp(_mode.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"BindGold",getCapital);
			bindManager.BindViewProp(_mode.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Score",getCapital);
			bindManager.BindViewProp(_mode.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Contribute",getCapital);
			bindManager.BindViewProp(_mode.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Shell",getCapital);
			bindManager.BindViewProp(_mode.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Spirit",getCapital);
			bindManager.BindViewProp(_mode.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"GuildContribute",getCapital);*/
		}
		
		/**
		 * 获得人物金钱资本
		 * @param key  Silver：  0	银两
							     1	银券
							     2	仙玉
							     3	仙石
							     4	积分
							     5	门派贡献
							     6	贝壳
							     7	灵
							     8	帮贡 
		 */
		private function getCapital(key:String,val:int):void
		{
			switch(key)
			{
				case "Silver":          //银两
				{
					capital[0]=val;
					break;
				}
				case "BindSilver":    // 绑定银
				{
					capital[1]=val;
					break;
				}
				case "Gold":            //仙玉
				{
					capital[2]=val;
					break;
				}
				case "BindGold":       //点券
				{
					capital[3]=val;
					break;
				}
				case "Score":           //积分
				{
					capital[4]=val;
					break;
				}
				case "Contribute":     //贡献
				{
					capital[5]=val;
					break;
				}
				case "Shell":           //贝壳
				{
					capital[6]=val;
					break;
				}
				case "Spirit":          //灵气
				{
					capital[7]=val;
					break;
				}
				case "GuildContribute"://帮贡
				{
					capital[8]=val;
					break;
				}
			}
			setHaveMoney();
		}
		/**
		 * 按钮监听事件
		 * 
		 */
		private function initEvent():void
		{
			preBtn.addEventListener(MouseEvent.CLICK,onPageUp);
			nextBtn.addEventListener(MouseEvent.CLICK,onPageDown);
			windowBG._closeBtn.addEventListener(MouseEvent.CLICK,onClose);
		}
		
		/**
		 * 商店初始化 
		 * 
		 */
		private function initshop():void
		{
			_goods = new Array();
			thePage = goodsInfo.page+1;
			maxPage = goodsInfo.totalpage;
			
			for(var i:int=0;i<goodsInfo.goods.length;i++)
			{
				_goods[i] = new ShopGoodsUI(goodsInfo.goods[i],capital[goodsInfo.goods[i].selltype]);
			}
			showShop();
		}
		/**
		 *更新玩家拥有的金钱
		 * 
		 */
		private function setHaveMoney():void
		{
			for(var i:int=0;i<_goods.length;i++)
			{
				if(null != _goods[i])
				{
					ShopGoodsUI(_goods[i]).haveMoney = capital[ShopGoodsUI(_goods[i]).good.selltype];
				}
			}
		}
	
		/**
		 * 显示本页商品
		 * 
		 */
		private function showShop():void
		{
			//判断翻页按钮状态
			preBtn.mouseEnabled  = thePage!=1;
			nextBtn.mouseEnabled = thePage!=maxPage;
			pageLabel.htmlStrByBorder = thePage+"/"+maxPage;
			//循环显示商品信息
			for(var i:int=0;i<col*row && i<_goods.length;i++)
			{
				_goods[i].x = 35+(i%col)*205;
				_goods[i].y = 40+int(i/col)*100;
				windowBG.addChild(_goods[i]);
			}
		}
		/**
		 * 移除前页商品
		 * 
		 */
		public function removeGoods():void
		{
			for(var i:int=0;i<_goods.length;i++)
				windowBG.removeChild(_goods[i]);
		}
		/**
		 * 点击上一页,显示上一页内容
		 * 
		 */
		private function onPageUp(e:MouseEvent):void
		{
			if(thePage>1)
			{
				removeGoods();
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_TRADE_PAGE	,npcID,thePage-2);
			}
		}
		/**
		 * 点击下一页，显示下一页内容
		 * 
		 */
		private function onPageDown(e:MouseEvent):void
		{
			if(thePage<maxPage)
			{
				removeGoods();
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_TRADE_PAGE	,npcID,thePage);
			}
		}

		/**
		 * 弹出输入框，输入所选择商品的购买数量 
		 * 
		 */
		public function setAmount(good:Object):void
		{
			amountUI = InputAmountUI.getInstance();
			this.addChild(amountUI);
			amountUI.setGoodInfo(good,good!=null?capital[good.selltype]:null);
		}
		/**
		 * 关闭商店窗口
		 * 
		 */
		public function onClose(e:MouseEvent):void
		{
			if(this.parent != null)
			{
				this.parent.removeChild(this);
			}
			_self=null;
		}
		/**
		 * 获取商品信息
		 * 
		 */
		public function getGoods(evt:CustomToClientEvent):void
		{
			//获取交易npc信息
		/*	npcID     =  evt.pList.shift();
			//获取交易商品信息
			goodsInfo =  com.adobe.serialization.json.JSON.decode(evt.pList.shift());*/
			
			initshop();
			if(goodsInfo.page==0)
			{
				show();
			}
		}
	}
}
